<?php

class UserController extends BaseController
{
    public function postLogin()
    {
        $email = Input::get('email');
        $password = Input::get('password');

        echo $email . $password;
        $data = array(
            'email' => $email,
            'password' => $password
        );

        if (Auth::attempt($data)) {
            return Redirect::to('/');
        } else {
            return Redirect::to('user/login')->with('error', '用户名或者密码错误');
        }
    }

    public function postRegister()
    {
        $data = Input::all();
        $user = new Users;

        $isExist = DB::table('users')->where('name', '=', $data['name'])->get();
        if ($isExist) {
            return Redirect::to('user/register')->with('nameError', '用户名已存在');
        }

        $isExist = DB::table('users')->where('email', '=', $data['email'])->get();
        if ($isExist) {
            return Redirect::to('user/register')->with('emailError', '邮箱已存在');
        }
        if ($data['password'] != $data['password2']) {
            return Redirect::to('user/register')->with('passError', '两次密码不一致');
        }


        $user->name = $data['name'];
        $user->email = $data['email'];
        $user->password = Hash::make($data['password']);
        $user->save();

        $gameUser = new User;
        $gameUser->name = $data['name'];
        $gameUser->save();
        return Redirect::to('user/login')->with('success', '注册成功');
    }


    public function getLogin()
    {
        return View::make('login');
    }

    public function getRegister()
    {
        return View::make('register');
    }

    public function getLogout()
    {
        Auth::logout();
        return Redirect::to('/');
    }


    public function getTest()
    {
        var_dump(user::find(1)->first());
    }

}
